//
//  ZCHomeViewController.swift
//  ZYDY
//
//  Created by zhangchao on 2017/8/17.
//  Copyright © 2017年 zhangchao. All rights reserved.
//

import UIKit

private let kTitleViewH : CGFloat = 40
private let kTitleCount = 3

class ZCHomeViewController: UIViewController {

    fileprivate lazy var pageTitleView : ZCPageTitleView = {[weak self] in
        let titleFrame = CGRect(x: 0, y: 64, width: kScreenW, height: kTitleViewH)
        let titles = ["推荐", "游戏", "娱乐", "趣玩"]
        let titleView = ZCPageTitleView(frame: titleFrame, titles: titles)
        titleView.delegate = self
        return titleView
    }()
    
    fileprivate lazy var pageContentView : ZCPageContentView = {
        let contentH = kScreenH - (kStatusBarH + kNavgationBarH + kTabbarH + kTitleViewH);
        let contentFrame = CGRect(x: 0, y: kStatusBarH + kNavgationBarH + kTitleViewH, width: kScreenW, height: contentH)
        var childVcs = [UIViewController]()
        childVcs.append(ZCRecommendViewController())
        for _ in 0..<kTitleCount {
            let vc = UIViewController()
            vc.view.backgroundColor = UIColor(r: CGFloat(arc4random_uniform(255)), g: CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255)), alpha: 1.0)
            childVcs.append(vc)
        }
        let contentView = ZCPageContentView(frame: contentFrame, childVcs: childVcs, parentViewController: self)
        contentView.delegate = self
        return contentView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        setupUI()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

// MARK: - 设置UI
extension ZCHomeViewController {
    fileprivate func setupUI() {
        setupNavgationBar()
        view.addSubview(pageTitleView)
        view.addSubview(pageContentView)
    }
    
    private func setupNavgationBar() {
        setupNavgationBarLeftBar()
        setupNavgationBarRightBar()
    }
    
    private func setupNavgationBarLeftBar() {
        let logoBtn = UIBarButtonItem(imageName: "homeLogoIcon", highImageName: "homeLogoIcon", size: CGSize.zero, target: self, action:#selector(self.logobtnClick))
        navigationItem.leftBarButtonItem = logoBtn
        
    }
    
    private func setupNavgationBarRightBar() {
        let size = CGSize(width: 30, height: 30)
        let qrcodeBtn = UIBarButtonItem(imageName: "home_newQrcode", highImageName: nil, size: size, target: self, action:#selector(self.qrcodebtnClick))
        let gameBtn = UIBarButtonItem(imageName: "home_newGameicon", highImageName: nil, size:size, target: self, action:#selector(self.gamebtnClick))
        let historyBtn = UIBarButtonItem(imageName: "home_newHistoryIcon", highImageName: nil, size: size, target: self, action:#selector(self.historybtnClick))
        navigationItem.rightBarButtonItems = [historyBtn, gameBtn, qrcodeBtn]
    }
    
}

// MARK: - ZCPageTieleViewDelegate
extension ZCHomeViewController : ZCPageTieleViewDelegate {
    func pageTitleView(titleView: ZCPageTitleView, selectedIndex index: Int) {
        print(index)
        pageContentView.setCurrentIndex(currentIndex: index)
    }
}

// MARK: - ZCPageContentViewDelegate
extension ZCHomeViewController : ZCPageContentViewDelegate {
    func pageContentView(contentView: ZCPageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        pageTitleView.setTitleWithProgress(progress: progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}

// MARK: - 首页点击事件
extension ZCHomeViewController {
    @objc fileprivate func logobtnClick() {
        print("点击logo")
    }
    
    @objc fileprivate func qrcodebtnClick() {
        print("点击二维码")
    }
    
    @objc fileprivate func gamebtnClick() {
        print("点击游戏")
    }
    
    @objc fileprivate func historybtnClick() {
        print("点击历史")
    }
    
}
